<!--
 * @Descripttion: 权限分配
 * @version: 
 * @Author: wl
 * @Date: 2020-06-06 15:23:29
 * @LastEditors: wl
 * @LastEditTime: 2020-06-06 18:16:14
--> 
<template>
  <div>
    <div class="dialog-toolbar">
      <el-button @click="handleClose">关闭</el-button>
      <el-button type="primary" @click="handleSave">保存</el-button>
      <!-- <el-button type="primary" @click="handleReset">重置</el-button> -->
    </div>
    <div class="dialog-content">
      <el-tabs v-model="activeName" type="card">
        <el-tab-pane label="分配权限" :name="type.ROLE">
          <menu-tree ref="menuTree" :checkedKeys="defaultCheckedMenuKeys" @check="handleMenuCheck"></menu-tree>
        </el-tab-pane>
        <!-- <el-tab-pane label="权限范围" :name="type.PERM_RANGE">
           <el-radio-group v-model="filters.dataPermissionType">
            <el-radio-button :label="dataPermissionType.ALL">全部</el-radio-button>
            <el-radio-button :label="dataPermissionType.SELF">自己</el-radio-button>
            <el-radio-button :label="dataPermissionType.SUB">所属机构及下属机构</el-radio-button>
            <el-radio-button :label="dataPermissionType.CORP_SELECT">单位</el-radio-button>
          </el-radio-group>
          <unit-tree ref="unitTree" v-if="filters.dataPermissionType===dataPermissionType.CORP_SELECT" :checkedKeys="defaultCheckedCorpKeys" @check="handleCorpCheck"></unit-tree>
        </el-tab-pane> -->
      </el-tabs>
    </div>
  </div>
</template>

<script>

import MenuTree from './MenuTree'
// import UnitTree from './components/UnitTree'

export default {
  name: 'PermPage',
  components: {
    MenuTree,
    // UnitTree
  },
  props: {
    data: {
      type:Object,
      default:()=>{}
    }
  },
  data(){
    return {
      activeName: '1',
      type: {
        ROLE: '1', //分配权限
        PERM_RANGE: '2' //权限范围
      },
      loading: false,
      defaultCheckedMenuKeys: [],
      isMenuCheck: false,
      filters: {
        resourceIds: [],
        dataPermissionType: '',
        corpIds: []
      },
      dataPermissionType: { //1.全部；2.自己；3.所属机构及下属机构；4.指定关联列表
        ALL:1,
        SELF:2,
        SUB: 3,
        CORP_SELECT: 4
      },
      isCorpCheck: false,
      defaultCheckedCorpKeys: []
    }
  },
  methods: {
    // 获取权限资源范围
    handleQuery(){
      this.loading = true;
      let params={
        where:{_id:this.data._id,show:true}
      }
      
      let param= JSON.stringify(params); 
      this.$api.role.getPermIds(param).then((res) => {
        if(res.code == 0){
          this.defaultCheckedMenuKeys = res.data
        }else{
          this.defaultCheckedMenuKeys = [];
        }
      })
        console.log(defaultCheckedMenuKeys);
    },
  
    // 获取角色权限范围
    initPermData(){
      this.filters.dataPermissionType = this.data.dataPermissionType;
      this.defaultCheckedCorpKeys = this.data.corpIdList;
    },
    handleClose(){
      this.$emit('handleClose')
    },
    handleSave(){
      let params = {
        rows: {right_indexs:this.filters.resourceIds },
       where:{_id:this.data._id} //1.全部；2.自己；3.所属机构及下属机构；4.指定关联列表
      }


        let param= JSON.stringify(params); 
      // if(!this.isMenuCheck){
      //   params.resourceIds = this.defaultCheckedMenuKeys.join(',')
      // }else {
      //   params.resourceIds = this.filters.resourceIds.join(',')
      // }
      // if(this.filters.dataPermissionType ===this.dataPermissionType.CORP_SELECT){
      //   if(!this.isCorpCheck){
      //     params.corpIds = this.defaultCheckedCorpKeys.join(',')
      //   }else {
      //     params.corpIds = this.filters.corpIds.join(',')
      //   }
      // }else{
      //   params.corpIds = ''
      // }
        // this.$api.menu.findMenuTree(param).then(res => {
      this.$api.role.savePerm(param).then((res)=> {
        if(res.code == 0){
          this.$message.success('设置成功')
          this.$emit('handleSave')
          this.handleClose()
        }else {
          this.$message.error('设置失败，'+res.msg)
        }
      })
    },
    handleReset(){
      if(this.activeName === this.type.ROLE){
        this.$refs.menuTree.setCheckedKeys();
      }else {
        this.filters.dataPermissionType = this.data.dataPermissionType;
        if(this.filters.dataPermissionType === this.dataPermissionType.CORP_SELECT){
          if(this.$refs.unitTree){
            this.$refs.unitTree.setCheckedKeys()
          }
        }
      }
    },
    handleMenuCheck(ids){
      this.isMenuCheck = true
      this.filters.resourceIds = ids;
      
    },
    handleCorpCheck(ids){
      this.isCorpCheck = true
      this.filters.corpIds = ids
    }
  },
  mounted(){
    this.handleQuery()
    this.initPermData()
  }
}
</script>